package org.example.designmodel.future;

import org.example.utils.LogUtils;

public class ThreadJoinTest {

    private final static int TLEEP_CAP = 2000;

    public static void main(String[] args) {
        try {
            // 先等烧水、洗杯子完成后，才能有水喝
            Thread hotWaterThread = new Thread(new HotWater());
            Thread washCupThread = new Thread(new WashCup());

            hotWaterThread.start();
            washCupThread.start();

            hotWaterThread.join();
            washCupThread.join();

            // 等待上述两个线程执行完毕，才能进行主线程操作
            LogUtils.info("泡茶喝");
        } catch (InterruptedException e) {
            LogUtils.info("主线程失败");
        }
    }

    /**
     * 烧水线程
     */
    static class HotWater implements Runnable{
        @Override
        public void run() {
            try {
                LogUtils.info("洗好水壶");
                LogUtils.info("灌上凉水");
                LogUtils.info("放在火上");
                Thread.sleep(TLEEP_CAP);
                LogUtils.info("水开了");
            } catch (InterruptedException e) {
                LogUtils.info("烧水报错了");
            }
        }
    }

    /**
     * 洗杯子线程
     */
    static class WashCup implements Runnable {
        @Override
        public void run() {
            try {
                LogUtils.info("洗茶壶");
                LogUtils.info("洗茶杯");
                LogUtils.info("拿茶叶");
                Thread.sleep(TLEEP_CAP);
                LogUtils.info("洗完了");
            } catch (InterruptedException e) {
                LogUtils.info("洗茶壶失败了");
            }
        }
    }
}
